﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using HROReporting.Model.Docenten;
using HROReporting.Infrastructure.UnitOfWork;
using NHibernate;
using NHibernate.Criterion;
using HROReporting.Infrastructure.Querying;
using HROReporting.Model.PresentieIncheckingen;

namespace HROReporting.Repository.NHibernate.Repositories
{
    public class DocentRepository : Repository<Docent, int>, IDocentRepository
    {
        public DocentRepository(IUnitOfWork uow)
            : base(uow)
        {
        }

        public Presentielijst GetPresentielijstByPresentielijstId(int presentielijstId)
        {
            ICriteria criteriaQuery = SessionFactory.GetCurrentSession()
                .CreateCriteria(typeof(Presentielijst))
                .Add(Expression.Eq(PropertyNameHelper
                .ResolvePropertyName<Presentielijst>
                (d => d.Id), presentielijstId));

            IList<Presentielijst> presentielijsten = criteriaQuery.List<Presentielijst>();
            Presentielijst presentielijst = presentielijsten.FirstOrDefault();
            return presentielijst;  
        }

        public Docent GetDocentByDocentCode(string docentCode)
        {
            ICriteria criteriaQuery = SessionFactory.GetCurrentSession()
                .CreateCriteria(typeof(Docent))
                .CreateAlias("DocentCode", "DocentCode")
                .Add(Expression.Eq(PropertyNameHelper
                .ResolvePropertyName<Docent>
                (d => d.DocentCode.Code), docentCode));

            IList<Docent> docenten = criteriaQuery.List<Docent>();
            Docent docent = docenten.FirstOrDefault();

            return docent;
        }

        public IEnumerable<Docent> GetDocentenByAchternaam(string achternaam)
        {
            ICriteria criteriaQuery = SessionFactory.GetCurrentSession()
                .CreateCriteria(typeof(Docent))
                .Add(Expression.Eq(PropertyNameHelper
                .ResolvePropertyName<Docent>
                (d => d.Achternaam), achternaam));

            IList<Docent> docenten = criteriaQuery.List<Docent>();

            return docenten;
        }

        public IEnumerable<Docent> GetDocentenByDocentCode(string docentCode)
        {
            ICriteria criteriaQuery = SessionFactory.GetCurrentSession()
                .CreateCriteria(typeof(Docent))
                .CreateAlias("DocentCode", "DocentCode")
                .Add(Expression.Eq(PropertyNameHelper
                .ResolvePropertyName<Docent>
                (d => d.DocentCode.Code), docentCode));

            IList<Docent> docenten = criteriaQuery.List<Docent>();

            return docenten;
        }

        public IEnumerable<Docent> GetDocentenBySchoolLocatie(string schoolLocatie)
        {
            ICriteria criteriaQuery = SessionFactory.GetCurrentSession()
                .CreateCriteria(typeof(Docent))
                .CreateAlias("SchoolLocatie", "SchoolLocatie")
                .Add(Expression.Eq(PropertyNameHelper
                .ResolvePropertyName<Docent>
                (d => d.SchoolLocatie.Locatie), schoolLocatie));

            IList<Docent> docenten = criteriaQuery.List<Docent>();

            return docenten;
        }

        public IEnumerable<Docent> GetDocentenByAchternaamTerm(string term)
        {
            ICriteria criteriaQuery = SessionFactory.GetCurrentSession()
                .CreateCriteria(typeof(Docent))
                .Add(Expression.Like("Achternaam", term+"%"));

            IList<Docent> docenten = criteriaQuery.List<Docent>();
            return docenten;
        }


        public IEnumerable<Docent> GetDocentenByDocentCodeTerm(string term)
        {
            ICriteria criteriaQuery = SessionFactory.GetCurrentSession()
                .CreateCriteria(typeof(Docent))
                .CreateAlias("DocentCode", "DocentCode")
                .Add(Expression.Like(PropertyNameHelper
                .ResolvePropertyName<Docent>
                (d => d.DocentCode.Code), term+"%"));

            IList<Docent> docenten = criteriaQuery.List<Docent>();
            return docenten;
        }

        public IEnumerable<Docent> GetDocentenBySchoolLocatieTerm(string term)
        {
            ICriteria criteriaQuery = SessionFactory.GetCurrentSession()
                .CreateCriteria(typeof(Docent))
                .CreateAlias("SchoolLocatie", "SchoolLocatie")
                .Add(Expression.Like(PropertyNameHelper
                .ResolvePropertyName<Docent>
                (d => d.SchoolLocatie.Locatie), term + "%"));

            IList<Docent> docenten = criteriaQuery.List<Docent>();
            return docenten;
        }

        public Docent GetDocentByAuthenticationToken(string authenticationToken)
        {
            ICriteria criteriaQuery = SessionFactory.GetCurrentSession()
                .CreateCriteria(typeof(Docent))
                .Add(Expression.Eq(PropertyNameHelper
                .ResolvePropertyName<Docent>
                (d => d.Email), authenticationToken));

            IList<Docent> docenten = criteriaQuery.List<Docent>();
            Docent docent = docenten.FirstOrDefault();

            return docent;
        }


        public Presentielijst GetPresentielijstById(int id)
        {
            throw new NotImplementedException();
        }

        public IEnumerable<PresentieInchecking> GetPresentieByLijstId(int id)
        {
            ICriteria criteriaQuery = SessionFactory.GetCurrentSession()
                .CreateCriteria(typeof(PresentieInchecking))
                .Add(Expression.Eq(PropertyNameHelper
                .ResolvePropertyName<PresentieInchecking>
                (d => d.PresentielijstId), id));

            IList<PresentieInchecking> presentieIncheckingen = criteriaQuery.List<PresentieInchecking>();
            return presentieIncheckingen;  
        }
    }
}
